﻿using System;
using System.Collections.Generic;
using System.Globalization;
using System.Text;

namespace HexUtilHelpV2
{

    public class HexToolV2
    {
        static decimal dHexValue = 255;
        /// <summary>
        /// 将百分比转换成十六进制
        /// </summary>
        /// <param name="number">80即表示80%</param>
        /// <param name="capacity">不足设定的位数的时候，向左边补充'0'</param>
        /// <returns></returns>
        public static string PercentToHex(decimal number, int capacity)
        {
            try
            {
                double _dPercent = ((double)number) / 100;
                double _sNumber = Math.Round(_dPercent * 255, 0);
                return string.Format("{0:x}", (int)_sNumber).PadLeft(capacity, '0');
            }
            catch (Exception ex)
            {
                throw new Exception(string.Format("PercentToHex Exception:{0}", ex.Message.Trim()));
            }
        }
        /// <summary>
        /// 将十六进制转换成百分比
        /// </summary>
        /// <param name="hex">需要转换的十六进制</param>
        /// <returns></returns>
        public static decimal HexToPercent(string hex)
        {
            decimal _dResult = -1;
            try
            {
                int _nPercent = -1;
                if (int.TryParse(hex, NumberStyles.HexNumber, CultureInfo.InvariantCulture, out _nPercent))
                    _dResult = Math.Round((_nPercent / dHexValue) * 100, 2);
                else
                    throw new Exception("PercentToHex Exception:十六进制值转换失败。");
            }
            catch (Exception ex)
            {
                throw new Exception(string.Format("HexToPercent Exception:{0}", ex.Message.Trim()));
            }
            return _dResult;
        }
        ///// <summary>
        ///// 将decimal转换成hex
        ///// </summary>
        ///// <param name="number">需要转换的decimal</param>
        ///// <param name="capacity">转换后hex长度</param>
        ///// <returns>hex数据</returns>
        //public static string ToHexString(decimal number, int capacity)
        //{
        //    return ToHexString((int)number).PadLeft(capacity, '0');
        //}
        /// <summary>
        /// 将hex转换成Decimal
        /// </summary>
        /// <param name="hex"></param>
        /// <returns></returns>
        public static decimal ConvertToDecimal(string hex)
        {
            return Convert.ToInt64(hex, 16);
        }
        /// <summary>
        /// 将Hex转换Int
        /// </summary>
        /// <param name="hex"></param>
        /// <returns></returns>
        public static int ConvertToInt(string hex)
        {
            return int.Parse(hex, NumberStyles.AllowHexSpecifier);
        }
        /// <summary>
        /// 将INT转换成hex
        /// </summary>
        /// <param name="number"></param>
        /// <param name="capacity"></param>
        /// <returns></returns>
        public static string ToHexString(int number, int capacity)
        {
            return ToHexString(number).PadLeft(capacity, '0');
        }
        /// <summary>
        /// 将INT转换成hex
        /// </summary>
        /// <param name="number"></param>
        /// <param name="capacity"></param>
        /// <returns></returns>
        public static string ToHexString(int number)
        {
            return string.Format("{0:x}", number);
        }
    }
}
